package com.antd.admin.server.controller;


import cn.hutool.core.util.StrUtil;
import com.antd.admin.api.entity.SysJob;
import com.antd.admin.server.service.ISysJobService;
import com.antd.common.core.util.R;
import com.antd.common.log.annotation.SystemLog;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 岗位管理 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/job")
@Api("岗位管理")
public class SysJobController {

    @Autowired
    private ISysJobService jobService;
    @GetMapping("/jobList")
    @SystemLog(description = "查询岗位")
    public R<Object> jobList(){
        return R.ok(jobService.list());
    }


    @GetMapping
    @SystemLog(description = "分页查询岗位列表")
    @ApiOperation("分页查询岗位列表")
    @PreAuthorize("hasAnyAuthority('sys:job:list')")
    public R<Object> findJobPage(Integer pageNum, Integer pageSize, SysJob job){
        return R.ok(jobService.findJobPage(new Page<>(pageNum,pageSize), Wrappers
                .<SysJob>lambdaQuery()
                .like(StrUtil.isNotBlank(job.getJobName()),SysJob::getJobName,job.getJobName())));
    }
    @PostMapping
    @SystemLog(description = "保存岗位")
    @ApiOperation("保存岗位")
    @PreAuthorize("hasAnyAuthority('sys:job:save')")
    public R<Object> saveJob(@RequestBody SysJob job){
        return R.ok(jobService.save(job));
    }
    @PutMapping
    @SystemLog(description = "修改岗位")
    @ApiOperation("修改岗位")
    @PreAuthorize("hasAnyAuthority('sys:job:update')")
    public R<Object> updateJob(@RequestBody SysJob job){
        return R.ok(jobService.updateById(job));
    }

    @DeleteMapping
    @SystemLog(description = "删除岗位")
    @ApiOperation("删除岗位")
    @PreAuthorize("hasAnyAuthority('sys:job:delete')")
    public R<Object> deleteJob(String ids){
        List<String> jobIdList = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        return R.ok(jobService.removeByIds(jobIdList));
    }
}
